from common.logger_helper import g_wlogger
from common.utils import  get_lastyear_mouth
from k3_data_report.business.cgtj_db import Cgtj
from k3_data_report.business.chtj_process import compare_data_jl, baifenbi_data_jl
from k3_data_report.business.config import bussiness_cal_fields

__author__ = "Ypf"
_date_ = "2019/7/23-14:47"




def cgti_gys(time1):
    time2=get_lastyear_mouth(time1)
    db_sql = bussiness_cal_fields['k3_caozuo']
    check_sql=db_sql["check_sql"]%('k3cgtjgy',time1)
    check_dic=Cgtj.check_cf(check_sql)
    if len(check_dic)==0:
        sql1_gys = db_sql["cgdd_cgtj_gys"] % (time1)
        sql2_gys = db_sql["cgdd_cgtj_gys"] % (time2)
        sql3_gys=db_sql["insert_cgtj_gys"]
        cgtj_dic1_gys = Cgtj.get_dic_gys(sql1_gys)
        cgtj_dic2_gys = Cgtj.get_dic_gys(sql2_gys)
        cgtj_dic_gys = compare_data_jl(cgtj_dic1_gys, cgtj_dic2_gys)
        cgtj_gys=baifenbi_data_jl(cgtj_dic_gys)
        Cgtj.insert_cg(cgtj_gys,sql3_gys,time1)
    else:g_wlogger.winfo(f"cgti_gys-{time1}:target_full_df, all data is empty.")
def cgti_pp(time1):
    time2 = get_lastyear_mouth(time1)
    db_sql = bussiness_cal_fields['k3_caozuo']
    check_sql = db_sql["check_sql"] % ('k3cgtjpp', time1)
    check_dic = Cgtj.check_cf(check_sql)
    if len(check_dic)==0:
        sql1_pp = db_sql["cgdd_cgtj_pp"] % (time1)
        sql2_pp = db_sql["cgdd_cgtj_pp"] % (time2)
        sql3_pp = db_sql["insert_cgtj_pp"]
        cgtj_dic1_pp = Cgtj.get_dic_gys(sql1_pp)
        cgtj_dic2_pp = Cgtj.get_dic_gys(sql2_pp)
        cgtj_dic_pp= compare_data_jl(cgtj_dic1_pp, cgtj_dic2_pp)
        cgtj_pp=baifenbi_data_jl(cgtj_dic_pp)
        Cgtj.insert_cg(cgtj_pp,sql3_pp,time1)
    else:
        g_wlogger.winfo(f"cgti_pp-{time1}:target_full_df, all data is empty.")

def cgti_gys_main(month):
    try:
        cgti_gys(month)
        return True
    except Exception as e:
        g_wlogger.werror(f"cgti_gys_main has error:{e} with:{month}")
        return False
def cgti_pp_main(month):
    try:
        cgti_pp(month)
        return True
    except Exception as e:
        g_wlogger.werror(f"cgti_pp_main has error:{e} with:{month}")
        return False

if __name__ == "__main__":
    time1='2019-09'
    time2='2018-09'
    cgti_gys(time1)
    cgti_pp(time1)
